package com.infore.airsupervise.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.biz.MissionHandleBiz;
import com.infore.airsupervise.biz.impl.MissonHandleBizImpl;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JumpUtil;
import com.rey.material.widget.RadioButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * @features: 任务处理 界面(修改)
 * @author: WuTao
 * @date: 2017/4/18 15:03
 */
public class NewMissionHandleActivity extends BaseTitleBarActivity {
    @BindView(R.id.et_mision_handle_idea)
    EditText etMisionHandleIdea;
    @BindView(R.id.ll_handle)
    LinearLayout llHandle;
    @BindView(R.id.rb_enter)
    RadioButton rbEnter;
    @BindView(R.id.rb_noEnter)
    RadioButton rbNoEnter;
    @BindView(R.id.et_mision_examine_idea)
    EditText etMisionExamineIdea;
    @BindView(R.id.ll_examine)
    LinearLayout llExamine;

    private String userId;
    private int missionStatus;
    private String missionId;

    private MissionHandleBiz handleBiz; //处理、审核业务类
    private SweetAlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_miision_handle);
        getWindow().setBackgroundDrawable(null);
        ButterKnife.bind(this);
        Intent it = getIntent();
        userId = UserE.getUserInfo().getUserId();
        handleBiz = new MissonHandleBizImpl();
        rightMenu.setFontAwesomeIcon(FontAwesome.FA_CHECK);
        try {
            missionStatus = it.getIntExtra(Constants.INTENT_INT_KEY, 0);
            missionId = it.getStringExtra(Constants.INTENT_STR_KEY);
            if (missionStatus == StatusConst.MISSION_PROCESS_1) {
                title.setText("任务处理");
                llExamine.setVisibility(View.GONE);
                llHandle.setVisibility(View.VISIBLE);
            } else if (missionStatus == StatusConst.MISSION_PROCESS_5) {
                title.setText("任务审核");
                llExamine.setVisibility(View.VISIBLE);
                llHandle.setVisibility(View.GONE);
                rbEnter.setOnCheckedChangeListener(checkListener);
                rbNoEnter.setOnCheckedChangeListener(checkListener);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 任务处理
     */
    private void missionHandle() {
        handleBiz.commitMissionHandleRequest(userId, missionId, etMisionHandleIdea.getText().toString(), listener);
    }

    /**
     * 任务审核
     */
    private void missionExamine() {
        int isEnter = rbEnter.isChecked() ? 1 : 0;
        handleBiz.commitMissionExamineRequest(userId, missionId, etMisionExamineIdea.getText().toString(),
                isEnter, listener);
    }

    private OnServerResponseListener listener = new OnServerResponseListener<BaseResponse>() {
        @Override
        public void onSucceed(BaseResponse result, String desc) {
            //成功提示dialog
            dialog.setTitleText(MsgConst.OPERATION_SUCCESS)
                    .setContentText(desc)
                    .setConfirmText(MsgConst.I_SEE)
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismiss();
                            returnForResult(0);
//                            switch (missionStatus) {
//                                case StatusConst.MISSION_STATUS_FINISH:
//                                    returnForResult(StatusConst.MISSION_STATUS_EXAMINE);
//                                    break;
//                                case StatusConst.MISSION_STATUS_HANDLE:
//                                    returnForResult(StatusConst.MISSION_STATUS_FINISH);
//                                    break;
//                            }
                        }
                    })
                    .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
        }

        @Override
        public void onFailed(int errorCode, String title, String desc) {
            //失败提示dialog
            dialog.setTitleText(title)
                    .setContentText(desc)
                    .setConfirmText(MsgConst.I_SEE)
                    .changeAlertType(SweetAlertDialog.WARNING_TYPE);
        }
    };

    @Override
    protected void onRightMenuClick(View view) {
        super.onRightMenuClick(view);
        //loading dialog
        dialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(MsgConst.HARD_LOADING)
                .setContentText(MsgConst.PLEASE_WAITING);
        dialog.setCancelable(false);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if (null != dialog) {
                    dialog = null;
                }
            }
        });
        dialog.show();
        switch (missionStatus) {
            case StatusConst.MISSION_PROCESS_5:
                missionExamine();
                break;
            case StatusConst.MISSION_PROCESS_1:
                missionHandle();
                break;
        }
    }

    /**
     * 是否审核通过
     */
    private CompoundButton.OnCheckedChangeListener checkListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
                rbNoEnter.setChecked(compoundButton == rbNoEnter);
                rbEnter.setChecked(compoundButton == rbEnter);
            }
        }
    };

    /**
     *
     */
    private void returnForResult(int missionStatus) {
        Intent it = new Intent();
        it.putExtra(Constants.INTENT_INT_KEY, missionStatus);
        setResult(RESULT_OK, it);
        finish();
    }

    @OnClick(R.id.tv_siteLaw)
    public void onViewClicked() {
        JumpUtil.jumpFromTo(ctx,SiteMainCreateActivity.class , "intent_missionId" , missionId);
    }
}
